package com.yuanian.ecsplus.meta.vo;

import com.github.pagehelper.Page;
import com.yuanian.dp.common.vo.CustomeFieldValueVo;
import com.yuanian.ecsplus.exception.EcsPlusException;
import com.yuanian.ecsplus.meta.BaseMeta;
import com.yuanian.ecsplus.model.vo.BaseVoModel;
import com.yuanian.ecsplus.page.Pagination;
import org.springframework.beans.BeanUtils;
import java.util.*;

/**
 * vo持久化元数据
 * @param <T>
 */
public class PersistenceMetaVo<T extends BaseVoModel> extends BaseMeta<T> implements MetaVo<T> {

    public PersistenceMetaVo(Class<T> clz) {
        super(clz);
    }

    @Override
    protected String getObjectId(T obj) {
        try {
            Object id = this.getMetaClass().getIdField().getField().get(obj);
            if (id == null) {
                return null;
            }
            CustomeFieldValueVo idVo = (CustomeFieldValueVo)id;
            if (idVo.getFieldValue() == null) {
                return null;
            }
            return (String)idVo.getFieldValue();
        } catch (IllegalAccessException e) {
            throw new EcsPlusException(e.getCause());
        }
    }

    public List<Map> findFieldListWithValueMap(String sql) {
        List<Map> result = new ArrayList<>();
        //是否有记录
        List<Map> listMapData = platformSupport.selectBoDataList(getApiName(), null, sql);
        if (listMapData != null) {
            for (Map mapData : listMapData) {
                this.platformSupport.boMapFullDataConvert(mapData, getApiName());
                result.add(mapData);
            }
        }
        return result;
    }

    public Pagination<Map> findFieldListWithValueMap(String sql, int limit, int offset, boolean totalResults, String orderBy) {
        List<Map> listMapData = platformSupport.selectBoDataList(getApiName(), null, sql, limit, offset, totalResults, orderBy);
        if (listMapData == null) {
            return null;
        }
        for (Map mapData : listMapData) {
            this.platformSupport.boMapFullDataConvert(mapData, getApiName());
        }
        Page page = (Page) listMapData;

        Page<Map> result = new Page();
        BeanUtils.copyProperties(page, result);
        result.clear();

        for (Map mapData : listMapData) {
            result.add(mapData);
        }
        return new Pagination(result);
    }

    public Pagination<T> findFieldListWithValue(String sql, int limit, int offset, boolean totalResults, String orderBy) {
        List<Map> listMapData = platformSupport.selectBoDataList(getApiName(), null, sql, limit, offset, totalResults, orderBy);
        if (listMapData == null) {
            return null;
        }
        for (Map mapData : listMapData) {
            this.platformSupport.boMapFullDataConvert(mapData, getApiName());
        }
        Page page = (Page) listMapData;

        Page<T> result = new Page();
        BeanUtils.copyProperties(page, result);
        result.clear();

        for (Map mapData : listMapData) {
            result.add((T) this.getMetaClass().toEntity(mapData));
        }
        return new Pagination(result);
    }

    @Override
    public T selectBoDataByBoMainId(String ObjectId) {
        Map map = platformSupport.selectBoDataByBoMainId(getApiName(), null, ObjectId);
        if (map == null) {
            return null;
        }
        this.platformSupport.boMapFullDataConvert(map, getApiName());
        return (T) this.getMetaClass().toEntity(map);
    }

    @Override
    public List<T> findList(String sql) {
        List<T> result = new ArrayList<>();
        //是否有记录
        List<Map> listMapData = platformSupport.selectBoDataList(getApiName(), null, sql);
        if (listMapData == null) {
            return null;
        }
        for (Map mapData : listMapData) {
            this.platformSupport.boMapFullDataConvert(mapData, getApiName());
            result.add((T) this.getMetaClass().toEntity(mapData));
        }
        return result;
    }

    @Override
    public T findFieldList(Map initData, boolean insertFlag) {
        //字段map
        Map map = findFieldListMap(initData, insertFlag);
        return (T) this.getMetaClass().toEntity(map);

    }

    @Override
    public Map findFieldListMap(Map initData, boolean insertFlag) {
        String apiName = getApiName();
        Map map = this.platformSupport.createBoData(apiName, initData);
        if (insertFlag) {
            this.platformSupport.insertBoData(apiName,null, map);
        }
        //Map
        this.platformSupport.boMapFullDataConvert(map, apiName);
        return map;
    }

    @Override
    public int batchInsert(List<T> obj) {
        List<Map> list = new ArrayList<>();
        //实体和map的对应
        Map<T, Map> objMap = new HashMap();
        for (T t : obj) {
            Map m = this.getMetaClass().toMap(t);
            list.add(m);
            objMap.put(t, m);
        }
        int num = platformSupport.batchInsertBoData(getApiName(), null, list);
        //反写
        Set<Map.Entry<T, Map>> set = objMap.entrySet();
        for (Map.Entry<T, Map> e : set) {
            //得到实体
            Map mapData = e.getValue();
            platformSupport.boMapFullDataConvert(mapData, getApiName());
            this.getMetaClass().copyProperty(e.getKey(), mapData);
        }
        return num;
    }

}
